﻿using Microsoft.VisualBasic.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Common.Helpers
{
    //使用方法 ：  
    //创建IniFileHelper实例：
    //IniFileHelper iniFile = new IniFileHelper("文件路径");
    //    写入配置项：
    //iniFile.WriteValue("节名称", "键名称", "键值");
    //读取配置项：
    //string value = iniFile.ReadValue("节名称", "键名称");
    public class IniFileHelper
    {
        private string filePath;

        public IniFileHelper(string filePath)
        {
            this.filePath = filePath;
        }

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public void WriteValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, filePath);
        }

        public string ReadValue(string section, string key)
        {
            StringBuilder retVal = new StringBuilder(255);
            GetPrivateProfileString(section, key, "", retVal, 255, filePath);
            return retVal.ToString();
        }


        //第二种方法
        //使用方法
        //Dictionary<string, Dictionary<string, string>> iniData = ParseIniFile("config.ini");
        //foreach (KeyValuePair<string, Dictionary<string, string>> section in iniData)
        //{
        //    Console.WriteLine("[{0}]", section.Key);

        //    foreach (KeyValuePair<string, string> kvp in section.Value)
        //    {
        //        Console.WriteLine("{0}={1}", kvp.Key, kvp.Value);
        //    }

        //    Console.WriteLine();
        //}
        public Dictionary<string, Dictionary<string, string>> GetDictionary()
        {
            Dictionary<string, Dictionary<string, string>> iniData = new Dictionary<string, Dictionary<string, string>>();

            using (StreamReader reader = new StreamReader(filePath))
            {
                string section = "";
                Dictionary<string, string> sectionData = new Dictionary<string, string>();

                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine().Trim();

                    if (line.StartsWith("[") && line.EndsWith("]"))
                    {
                        // 开始一个新的节
                        section = line.Substring(1, line.Length - 2);
                        sectionData = new Dictionary<string, string>();
                        iniData[section] = sectionData;
                    }
                    else if (line.Contains("="))
                    {
                        // 解析键值对
                        string[] parts = line.Split('=');
                        string key = parts[0].Trim();
                        string value = parts[1].Trim();
                        sectionData[key] = value;
                    }
                }
            }

            return iniData;
        }
    }
}

//app.ini
//[Database]
//Server = 127.0.0.1
//Port = 3306
//Username = root
//Password = 123456
//Database = mydb

//[Logging]
//Level = INFO
//File =/ var / log / myapp.log
